//
//   BattleshipGameBoard.java
//  NetworkBattleship
//
//  Created by Alan Joyce on 12/17/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//
package gamefiles;

public class BattleshipGameBoard {
//Declare variables.
	int boardSize;
	public BattleshipGameCell[][] player1BoardCells;
	public BattleshipGameCell[][] player2BoardCells;
	
	
	public BattleshipGameBoard() {
		boardSize = 10;
	}
	
	
	public BattleshipGameBoard( int gameBoardSize) {
		boardSize = gameBoardSize;
	}
	
	public void createCells() {
		//boardCells[x pos][y pos]
		player1BoardCells = new BattleshipGameCell[boardSize][boardSize];
		player2BoardCells = new BattleshipGameCell[boardSize][boardSize];
		
		
		for( int i = 0 ; i < boardSize; i++) {
			for( int j = 0 ; j < boardSize; j++) {
				player1BoardCells[i][j] = new BattleshipGameCell(1);
			}
		}
		
		for( int i = 0 ; i < boardSize; i++) {
			for( int j = 0 ; j < boardSize; j++) {
				player2BoardCells[i][j] = new BattleshipGameCell(2);
			}
		}
		
	}
	
	public int getCellState(int player, int xPos, int yPos) {
		BattleshipGameCell[][] boardCells;
		
		if (player == 1) {
			boardCells = player1BoardCells;
		}
		else if (player == 2) {
			boardCells = player2BoardCells;
		}
		else {
			return -1;
		}
		
		return boardCells[xPos][yPos].getState();
	}
	
	public int setCellState(int player, int xPos, int yPos, int state) {
		BattleshipGameCell[][] boardCells;
		
		if (player == 1) {
			boardCells = player1BoardCells;
		}
		else if (player == 2) {
			boardCells = player2BoardCells;
		}
		else {
			return -1;
		}
		
		return boardCells[xPos][yPos].setState(state);
	}
	
	public int shipCellsHorizontal(int player, int startX, int stopX, int yPos) {
		BattleshipGameCell[][] boardCells;
		if(startX < 0 || yPos < 0 || stopX > boardSize || yPos > boardSize) {
			return -1;
		}
		
		if (player == 1) {
			boardCells = player1BoardCells;
		}
		else if (player == 2) {
			boardCells = player2BoardCells;
		}
		else {
			return -1;
		}
		
        for (int i = startX; i <= stopX; i++) {
            if(boardCells[i][yPos].getState() != BattleshipGameCell.STATE_EMPTY) {
                return -1;
            }
        }
        
        for (int i = startX; i <= stopX; i++) {
            boardCells[i][yPos].shipCell();
        }
        return 0;
	}
	
	
	public int shipCellsVertical(int player, int startY, int stopY, int xPos) {
		BattleshipGameCell[][] boardCells;
		if(startY < 0 || xPos < 0 || stopY > boardSize || xPos > boardSize) {
			return -1;
		}
		
		if (player == 1) {
			boardCells = player1BoardCells;
		}
		else if(player == 2) {
			boardCells = player2BoardCells;
		}
		else {
			return -1;
		}
		
        for (int i = startY; i <= stopY; i++) {
            if(boardCells[xPos][i].getState() != BattleshipGameCell.STATE_EMPTY) {
                return -1;
            }
        }
        
        for (int i = startY; i <= stopY; i++) {
            boardCells[xPos][i].shipCell();
        }
        return 0;
	}
	
	//Returns -1 if error, 0 if no ship hit, 1 if ship hit.
	public int fireCell(int player, int xPos, int yPos) {
		if(xPos < 0 || yPos < 0 || xPos > boardSize || yPos > boardSize) {
			return -1;
		}
		
		if(player == 1) {
			return player1BoardCells[xPos][yPos].fireCell();
		}
		else if(player == 2) {
			return player2BoardCells[xPos][yPos].fireCell();
		}
		else {
			return -1;
		}
	}
	
	public boolean checkVictoryCells(int player, int hitsNeeded) {
		int shipHitCount = 0;
		BattleshipGameCell[][] boardCells;
		
		if (player == 1) {
			boardCells = player2BoardCells;
		}
		else if (player == 2) {
			boardCells = player1BoardCells;
		}
		else {
			return false;
		}
		
		for(int i = 0 ; i < boardSize; i++) {
			for(int j = 0 ; j < boardSize; j++) {
				if(boardCells[i][j].getState() == BattleshipGameCell.STATE_SHIP_HIT) {
					shipHitCount++;
				}
			}
		}
        
		if(shipHitCount >= hitsNeeded) {
			return true;
		}
		else {
			return false;
		}
	}
}